﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Data;
using System.Globalization;

namespace BadHabit.WPFTools.Converters {
    
    [ValueConversion(typeof(object), typeof(Int16))]
    public class EnumConverter : IValueConverter {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            try {
                
                return Enum.Parse(parameter as System.Type, value.ToString(), true);
            } catch (Exception) { }
            
            return null;
        }
            
        
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            try {
                return (Int16)Enum.Parse(parameter as System.Type, value.ToString(), true);
            } catch (Exception) { }
            return null;
        } 
    }
}
